const app = getApp();
const Controller = require('../controllers/controllers.js').controller;

Page({
  data: {
      title:'搜索',
      defaultlistsbool:true,
      searchstory:[],
      isview:true, 
      inputValue:'',
      hotsings:[],
      focusbool:false,
      focuslists:[],
      focuspage:1,
      focuspageSize:10,
      focusisEnd:false
  }, 
  onLoad: function (options) {
    this.setData({
      inputValue:options.inputValue
    })
  },
  onShow() {
      //从本地获取搜索记录
      let searchstory = wx.getStorageSync('searchData1');
          console.log('从本地获取搜索记录searchstory',searchstory)

      if (searchstory && searchstory.length > 0) {
          //进行倒序
          searchstory = searchstory.reverse();
      }
      this.setData({
          searchstory: searchstory,
          inputValue: '',
          focusbool:false
      })
      this.searchKeywordList()
  },
  clearSearchStorage() {
      this.Dialog.ShowDialog({
          type: 'Confirm',
          title: '确定清除历史记录?',
          callback: res => {
              if (res.name == 'confirm') {
                  wx.setStorageSync('searchData1', []);
                  this.setData({
                      searchstory: []
                  });
                  
                  this.Dialog.CloseDialog();
              } else {
                  this.Dialog.CloseDialog();
              }
          }
      });
      
  },
  onReady() {
      this.Dialog = this.selectComponent('#Dialog');
  },
  onReachBottom: function () {

  },
  viewer(){
    this.setData({
      isview:!this.data.isview
    })
  },
  bindInput(e){
    var value=e.detail.value || ''
    if(value){
      this.setData({
        focusbool:true,
        focuslists:[],
        focuspage:1,
        inputValue: value
      })
    }else{
      this.setData({
        focusbool:false
      })
    }
    this.categoryList()
  },
  clearSearchStorage(){
    this.Dialog.ShowDialog({
        type: 'Confirm',
        title: '确定清除历史记录?',
        callback: res => {
            if (res.name == 'confirm') {
                wx.setStorageSync('searchData1', []);
                this.setData({
                    searchstory: []
                });
                
                this.Dialog.CloseDialog();
            } else {
                this.Dialog.CloseDialog();
            }
        }
    });
  },
  searchvalue(e) {
    var value=e.currentTarget.dataset.search;
    wx.navigateTo({
      url: '/pages/column/lists?search=' + value
    });
  },
  searchvalue2(e) {
    var _SearchValue=e.currentTarget.dataset.search;
    let localStorageValue = [];
    if (_SearchValue != '') {

        //添加搜索历史记录
        //调用API从本地缓存中获取数据
        localStorageValue = wx.getStorageSync('searchData1') || [];

        let tempSearchData = [];
        //过滤历史列表中相同的搜索记录
        for (let item of localStorageValue) {
            if (item != _SearchValue) {
                tempSearchData.push(item);
            }
        }
        //添加搜索文本进历史记录
        tempSearchData.push(_SearchValue);
        //保存至从本地缓存
        wx.setStorageSync('searchData1', tempSearchData);

        //跳转列表
        wx.navigateTo({
            url: "/pages/column/lists?search=" + _SearchValue
        });
    }
  },
  searchSubmit(e){
    var _SearchValue=e.detail.value;
    console.log('_SearchValue',e,_SearchValue)
    let localStorageValue = [];
    if (_SearchValue != '') {
        //添加搜索历史记录
        //调用API从本地缓存中获取数据
        localStorageValue = wx.getStorageSync('searchData1') || [];
        let tempSearchData = [];
        //过滤历史列表中相同的搜索记录
        for (let item of localStorageValue) {
            if (item != _SearchValue) {
                tempSearchData.push(item); 
            }
        }
        //添加搜索文本进历史记录
        tempSearchData.push(_SearchValue);
        //保存至从本地缓存
        wx.setStorageSync('searchData1', tempSearchData);
        //跳转列表
        wx.navigateTo({
            url: "/pages/column/lists?search=" + _SearchValue
        });
    }
  },
  delete(e){
    var _SearchValue=e.currentTarget.dataset.search;
    let localStorageValue = [];
    if (_SearchValue != '') {
        localStorageValue = wx.getStorageSync('searchData1') || [];
        var index = localStorageValue.indexOf(_SearchValue);
        localStorageValue.splice(index, 1)
        //保存至从本地缓存
        wx.setStorageSync('searchData1', localStorageValue);
        this.setData({
            searchstory: localStorageValue.reverse()
        });
    }
  },
  searchKeywordList(){
    Controller.searchKeywordList({
      limit:10
    }).then(res => {
      this.setData({
          hotsings:res.data.baseData
      });
      
    })
  },
  refesh(){
    this.searchKeywordList()
  },
  categoryList(){
    Controller.categoryList({
      keyword:this.data.inputValue,
      page:this.data.focuspage,
      pageSize:this.data.focuspageSize
    }).then(res => {
      if(res.data.baseData.list.length>0){
        for(let item of res.data.baseData.list){
          if(item.title){
            item.title1=item.title;
            item.title=item.title.replace(this.data.inputValue,"<span class='orange'>"+this.data.inputValue+"</span>");
          }
        }
        this.setData({
          focuslists:this.data.focuslists.concat(res.data.baseData.list)
        })
        if(res.data.baseData.lastPage){
          this.setData({
            focusisEnd:true
          })
        }
      }
    })
  },
  lower() {
    if (this.data.focusisEnd) {
      return;
    } else {
      this.setData({
          focuspage: this.data.focuspage + 1,
      });
      this.categoryList()
    }
  },
  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·栏目搜索',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·栏目搜索',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})